package com.hp.common.base;

import java.io.Serial;
import java.io.Serializable;

/**
 * 统一返回
 */
public class CommonResult<T> implements Serializable {

    @Serial
    private static final long serialVersionUID = -3569875314713220633L;

    private int code;
    private String msg;
    private T data;

    private CommonResult() {
    }

    public CommonResult(Type type, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public CommonResult(Type type, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
        if(data instanceof CommonError){
            CommonError error = (CommonError) data;
            this.code = error.getValue();
            this.msg = error.getName();
        }
    }

    public CommonResult(CommonError error){
        this.code = error.getValue();
        this.msg = error.getName();
    }

    public CommonResult(CommonError error,T data){
        this.code = error.getValue();
        this.msg = error.getName();
        this.data = data;
    }

    public enum Type{
        SUCCESS(0),
        FAIL(99999999)
        ;
        private final int value;

        Type(int value){
            this.value = value;
        }
    }

    /**
     * 返回成功
     * @param msg
     * @param data
     * @param <T>
     * @return
     */
    public static <T> CommonResult<T> success(String msg,T data){
        return new CommonResult<T>(Type.SUCCESS,msg,data);
    }

    /**
     * 返回成功
     * @param data
     * @param <T>
     * @return
     */
    public static <T> CommonResult<T> success(T data){
        return success("操作成功",data);
    }
    /**
     * 返回成功
     * @return
     */
    public static <T> CommonResult<T> success(){
        return success("操作成功",null);
    }

    /**
     * 返回失败
     * @param msg
     * @param data
     * @param <T>
     * @return
     */
    public static <T> CommonResult<T> fail(String msg,T data){
        return new CommonResult<T>(Type.FAIL,msg,data);
    }

    /**
     * 返回失败
     * @param data
     * @param <T>
     * @return
     */
    public static <T> CommonResult<T> fail(T data){
        return fail("操作失败",data);
    }

    /**
     * 返回失败
     * @return
     */
    public static <T> CommonResult<T> fail(){
        return fail("操作失败",null);
    }

    /**
     * 自定义异常返回
     * @param error
     * @param data
     * @param <T>
     * @return
     */
    public static <T> CommonResult<T> error(CommonError error,T data){
        return new CommonResult<T>(error,data);
    }

    /**
     * 自定义异常返回
     * @return
     */
    public static <T> CommonResult<T> error(CommonError error) {
        return error(error,null);
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }
}
